using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using BitMango;
using BitMango.Diagnostics;

using UnityEngine;

public class FirebaseAnalyticsHelper : MonoBehaviour
{
    //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private static event Action quitFunctions;

    public static string StageName
    {
        get
        {
            return FirebaseAnalyticsHelper.stageName;
        }
    }

    public static string Prefix
    {
        set
        {
            FirebaseAnalyticsHelper.prefix = value;
        }
    }

    public static float ElapseTime
    {
        get
        {
            return Time.time - FirebaseAnalyticsHelper.sessionStartTime;
        }
    }

    private void Awake()
    {
        base.StartCoroutine(FirebaseAnalyticsHelper.CoInitProperty());
    }

    private void onStartSession()
    {
        BitMango.Diagnostics.Debugger.Log("FirebaseAnalytics.onStartSession");
        FirebaseAnalyticsHelper.sessionStartTime = Time.time;
    }

    private IEnumerator onEndSession()
    {
        BitMango.Diagnostics.Debugger.Log("FirebaseAnalytics.onEndSession");
        float elapseTime = FirebaseAnalyticsHelper.ElapseTime;
        float dailySessionTime = Profile.DailySessionTime;
        float accumSessionTime = Profile.AccumSessionTime;
        Profile.AccumSessionTime = accumSessionTime;
        Profile.AccumSessionCount++;
        Profile.DailySessionCount++;
        PlayerPrefs.Save();
        yield return null;
        yield break;
    }

    private static IEnumerator CoInitProperty()
    {
        BitMango.Diagnostics.Debugger.Log("CoInitProperty in FirebaseAnalyticsHelper...");
        if (Platform.IsEU() && !Platform.IsTOSAgreed)
        {
            BitMango.Diagnostics.Debugger.Log("FirebaseAnalytics.logEvent - EU users who don't consent can't use FirebaseAnalyticsHelper");
            yield break;
        }
        while (!Platform.FinishedCheckFirebase)
        {
            yield return null;
        }
        if (!Platform.CheckAndFixedFirebase)
        {
            BitMango.Diagnostics.Debugger.Log("Can not use Firebasere in FirebaseAnalyticsHelper...");
            yield break;
        }
        BitMango.Diagnostics.Debugger.Log("Complete CoInitProperty in FirebaseAnalyticsHelper...");
        yield break;
    }

    private void Start()
    {
        BitMango.Diagnostics.Debugger.Log("FirebaseAnalytics.Start");
        this.onStartSession();
    }

    private IEnumerator OnApplicationPause(bool pause)
    {
        if (pause)
        {
            if (FirebaseAnalyticsHelper.loggingStageEvent)
            {
                PlayerPrefs.SetFloat("FirebaseAnalytics.cielp", Time.time - FirebaseAnalyticsHelper.startTime);
            }
            yield return base.StartCoroutine(this.onEndSession());
        }
        else
        {
            this.onStartSession();
        }
        yield break;
    }

    private void OnApplicationQuit()
    {
        this.onEndSession();
    }

    public static void logApplicationQuitEvent(string eventName, params object[] ps)
    {
        FirebaseAnalyticsHelper.quitFunctions += delegate ()
        {
            FirebaseAnalyticsHelper.logEvent(eventName, ps);
        };
    }

    public static void logEvent(string eventName, params object[] ps)
    {
        if (Platform.IsEU() && !Platform.IsTOSAgreed)
        {
            BitMango.Diagnostics.Debugger.Log("FirebaseAnalytics.logEvent - EU users who don't consent can't use FirebaseAnalyticsHelper");
            return;
        }
        eventName = eventName.Replace(".", "_");
        string text = "FirebaseAnalytics.logEvent:" + eventName;
        for (int i = 0; i < ps.Length; i++)
        {
            text += string.Format(" v{0}:{1}", i + 1, (ps[i] != null) ? ps[i].ToString() : string.Empty);
        }
        BitMango.Diagnostics.Debugger.Log(text);
    }

    private static IEnumerator CoLogEventToFirebase(string eventName, params object[] ps)
    {
        int checkCount = 10;
        while (checkCount > 0)
        {
            checkCount--;
            yield return new WaitForSeconds(1f);
            if (Platform.FinishedCheckFirebase)
            {
                break;
            }
            BitMango.Diagnostics.Debugger.Log("Trying to check Firebasere...");
        }
        if (!Platform.CheckAndFixedFirebase)
        {
            BitMango.Diagnostics.Debugger.Log("Can not use Firebasere at CoLogEventToFirebase...");
            yield break;
        }
        yield break;
    }

    public static void startStageEvent(string stage)
    {
        if (PlayerPrefs.HasKey("FirebaseAnalytics.cielp"))
        {
            PlayerPrefs.DeleteKey("FirebaseAnalytics.cielp");
        }
        BitMango.Diagnostics.Debugger.Log(string.Concat(new object[]
        {
            "FirebaseAnalytics.startStageEvent: ",
            stage,
            " T:",
            Time.time
        }));
        BitMango.Diagnostics.Debugger.Assert(!FirebaseAnalyticsHelper.loggingStageEvent, "stageEvent can only one at a time! : " + FirebaseAnalyticsHelper.stageName + ":" + stage);
        FirebaseAnalyticsHelper.loggingStageEvent = true;
        FirebaseAnalyticsHelper.stageName = stage;
        Profile.LastStageName = FirebaseAnalyticsHelper.stageName;
        FirebaseAnalyticsHelper.startTime = Time.time;
    }

    public static void endStageEvent(bool state, int starCount = -1)
    {
        BitMango.Diagnostics.Debugger.Log(string.Concat(new object[]
        {
            "FirebaseAnalytics.endStageEvent : ",
            FirebaseAnalyticsHelper.stageName,
            " T:",
            Time.time,
            "(",
            Time.time - FirebaseAnalyticsHelper.startTime,
            ")"
        }));
        BitMango.Diagnostics.Debugger.Assert(FirebaseAnalyticsHelper.loggingStageEvent, "First, call startStageEvent ");
        if (!FirebaseAnalyticsHelper.loggingStageEvent)
        {
            return;
        }
        FirebaseAnalyticsHelper.loggingStageEvent = false;
        float num = Time.time - FirebaseAnalyticsHelper.startTime;
        if (!state)
        {
            starCount = -1;
        }
        FirebaseAnalyticsHelper.logEvent("clearinfo", new object[]
        {
            (!state) ? "0" : "1",
            num.ToString(),
            FirebaseAnalyticsHelper.stageName,
            starCount.ToString()
        });
        FirebaseAnalyticsHelper.stageName = string.Empty;
        PlayerPrefs.DeleteKey("FirebaseAnalytics.cielp");
    }

    public static void logItemUse(string item, int count, int total, string where)
    {
        FirebaseAnalyticsHelper.logEvent("UncleBill.Use", new object[]
        {
            item,
            count.ToString(),
            total.ToString(),
            Profile.LastStageName,
            where
        });
    }

    public static void logItemBuy(string item, int count, int total, string where)
    {
        FirebaseAnalyticsHelper.logEvent("UncleBill.Buy", new object[]
        {
            item,
            count.ToString(),
            total.ToString(),
            Profile.LastStageName,
            where
        });
    }

    public static void logItemEarn(string item, int count, int total, string where)
    {
        FirebaseAnalyticsHelper.logEvent("UncleBill.Earn", new object[]
        {
            item,
            count.ToString(),
            total.ToString(),
            Profile.LastStageName,
            where
        });
    }

    private static float startTime;

    private static string stageName = string.Empty;

    public static float sessionStartTime;

    private static string prefix = string.Empty;

    private static bool loggingStageEvent;

}
